// app.js
// 引入工具对象文件
import './utils/utils'
import './utils/http'
App({
  //-------全局的属性-begin--------------
  token: '',
  refreshToken: '',
  //----------全局的属性-end-----------
  onLaunch() {
    this.getToken()
    this.getRefreshToken()
  },
  // 1. 从本地读取token和refreshToken
  getToken() {
    wx.getStorage({
      key: 'token',
      success: (res) => {
        this.token = res.data
      },
    })
  },
  getRefreshToken() {
    wx.getStorage({
      key: 'refreshToken',
      success: (res) => {
        this.refreshToken = res.data
      },
    })
  },
  // 2. 存储token和refreshToken到全局和本地
  setToken(token, refreshToken) {
    // 2.1 处理token, refreshToken
    token = `Bearer ${token}`
    refreshToken = `Bearer ${refreshToken}`
    // 2.2 存储到本地
    wx.setStorageSync('token', token)
    wx.setStorageSync('refreshToken', refreshToken)
    // 2.3 存储到全局
    this.token = token
    this.refreshToken = refreshToken
  },
})
